﻿using AntDesign;
using AntDesign.TableModels;
using Microsoft.AspNetCore.Components;
using Modules.Core.Domain.DomainServices.AntDesignExt;
using Modules.Core.Domain.TableDataModel;
using Modules.Core.Domain.TableModels;
using Modules.Core.Shared.PageDto;
using Modules.Template.AppServices.CodeTemplate;
using Modules.Template.Shared.CodeTemplate;

namespace Modules.Template.Blazor.Pages.CodeTemplate
{
    partial class CodeTemplateList2
    {
        [Parameter] public string ObjectId { get; set; } //如果对象ID参数，那么使用对象的管理权限
        //注入服务
        [Inject] private ICodeTemplatePageService _Service { get; set; }

        private DialogVM Dialog = new DialogVM();
        private TableDataModel<CodeTemplateVM> TableDataModel { get; set; } = new TableDataModel<CodeTemplateVM>(); //这里使用无参数构造函数，为了AntTable初始化
        private AntDesign.Table<CodeTemplateVM>? AntTable; //控件的@ref
        private TableOptions TableOptions { get; set; } = new TableOptions();
        bool IsAdd = false;
        private string PageTitleString = "CodeTemplate2";
        private CodeTemplateVM EditRow = new CodeTemplateVM();

        //Model对话框组件返回事件
        private void OnValueCallback(CodeTemplateVM IsSuccess)
        {
            Dialog.Visible = false;
        }
        //清除排序和筛选
        public void ResetTable()
        {
            AntTable?.ResetData();
        }

        protected override async Task OnInitializedAsync()
        {
            //if (string.IsNullOrWhiteSpace(ObjectId)) ObjectId = "CodeTemplateList2"; //指定这个页面的对象ID，用于用户自定义此页面参数（需要注册到对象管理器，得到对象ID)
            //var ret = await _Service.InitAsync(ObjectId); //注意必须初始化,指定唯一ID
            //this.TableDataModel = _Service.TableDataModel;

            await base.OnInitializedAsync();
        }
        private string UniqueID = "CodeTemplate"; //使用数据表作为，当前页面的唯一ID。
        private async Task TableDataModelInit()
        {
            if (string.IsNullOrWhiteSpace(ObjectId)) this.ObjectId = UniqueID; 
            var ret = await _Service.InitAsync(ObjectId); //注意必须初始化,指定唯一ID
            if (ret.Code == Core.Shared.ResultType.Success)
            {
                this.TableDataModel = _Service.TableDataModel;

                //增加默认筛选条件(不能叠加)
                //Expression<Func<CodeTemplateVM, bool>> expression = s => s.Createuid == "test";
                //TableDataModel.AttachWhereClause(expression); 
            }
            else
            {
                _ = _message.Error($@"初始化失败。" + ret.Msg);
            }
        }

        //AntDesign Table加载事件
        async Task HandleTableChangeAsync(QueryModel<CodeTemplateVM> queryModel)
        {
            if (!TableDataModel.IsValid())
            {
                await TableDataModelInit();
            }
            else
            {
                TableDataModel.TableModel.ConditionalModels = queryModel.FilterModel.ToSqlSugarConditional();
                TableDataModel.TableModel.OrderByModel = queryModel.SortModel.ToSqlSugarOrderBy();

                await this.FetchPageDataAsync();
            }
        }
        private async Task FetchPageDataAsync()
        {
            TableDataModel.TableModel.Loading = true;
            await _Service.QueryPageModelDataAsync();
            TableDataModel.TableModel.Loading = false;
            //在线程中更改了变量状态，需手动刷新。
            //await InvokeAsync(StateHasChanged);
            _ = _message.Success($@"当前第{TableDataModel.TableModel.PageIndex}页,合计{TableDataModel.TableModel.TotalCount}条数据。");

        }

        #region 设置表格宽度，默认显示列等信息。
        private bool userColumnDrawerVisible = false;
        private void SettingTable()
        {
            this.userColumnDrawerVisible = true;
        }
        //用户自定义列返回事件
        private async Task OnUserColumnDrawerCallbackAsync()
        {
            TableDataModel.TableOptions.AutoColumnwidth = true;
            this.userColumnDrawerVisible = false;
            await this.FetchPageDataAsync();
        }
        #endregion

        #region 增删改
        private void AddClick()
        {
            this.IsAdd = true;
            Dialog.Title = "添加数据";
            Dialog.Visible = true;
        }
        private void EditClick(CodeTemplateVM editRow)
        {
            this.IsAdd = false;
            this.EditRow = editRow;
            Dialog.Title = "编辑数据";
            Dialog.Visible = true;
        }
        private async Task DelClick(CodeTemplateVM editRow)
        {
            var confirmResult = await _confirmService.Show("确认删除这条数据吗？", "", ConfirmButtons.OKCancel);
            if (confirmResult == ConfirmResult.OK)
            {
                var ret = await _Service.RemoveRowDataAsync(editRow);
                _ = ret ? _message.Success("删除成功") : _message.Error("删除失败。");
            }
        }

        private async Task BatchDelClick()
        {
            if (TableDataModel.TableModel.SelectedRows == null || TableDataModel.TableModel.SelectedRows.Count() == 0)
            {
                _ = _message.Info("请选择需要删除的数据。");
                return;
            }

            var confirmResult = await _confirmService.Show("确认删除这条数据吗？", "", ConfirmButtons.OKCancel);
            if (confirmResult == ConfirmResult.OK)
            {
                var ret = await _Service.RemoveRowDataAsync(TableDataModel.TableModel.SelectedRows.ToList());
                _ = ret ? _message.Success("删除成功") : _message.Error("删除失败。");
            }
        }
        #endregion
    }
}
